import { DaHuaICCBaseService } from "./base";
import { dahua } from "./type";

/**
 * 大华ICC考勤管理服务
 * @description 提供考勤管理相关接口服务
 */
export class AttendanceService extends DaHuaICCBaseService {
	/**
	 * 查询打卡记录
	 * @description 根据条件查询考勤打卡记录
	 * @param data 查询参数
	 * @returns 打卡记录分页查询结果
	 */
	async queryAttendanceRecords(data: dahua.icc.attendance.IAttendanceRecordRequest) {
		// 参数验证
		if (!data.pageNum || data.pageNum <= 0) {
			throw new Error("页码必须大于0");
		}
		if (!data.pageSize || data.pageSize <= 0) {
			throw new Error("每页记录数必须大于0");
		}
		if (!data.actionTimeAfter) {
			throw new Error("开始时间不能为空");
		}
		if (!data.actionTimeBefore) {
			throw new Error("结束时间不能为空");
		}

		const url = `${this.config.baseUrl}/evo-apigw/evo-attendance/1.1.0/attendance/record/page`;
		const res = await this.curl<dahua.icc.IResponse<dahua.icc.attendance.IAttendanceRecordResponse>>(url, {
			method: "post",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
			data,
		});
		this.handleResponseException(res);
		return res.data.data;
	}

	/**
	 * 分页查询考勤结果
	 * @description 根据条件查询考勤结果记录
	 * @param data 查询参数
	 * @returns 考勤结果分页查询结果
	 */
	async queryAttendanceResults(data: dahua.icc.attendance.IAttendanceResultRequest) {
		// 参数验证
		if (!data.pageNum || data.pageNum <= 0) {
			throw new Error("页码必须大于0");
		}
		if (!data.pageSize || data.pageSize <= 0) {
			throw new Error("每页记录数必须大于0");
		}
		if (!data.dutyDateAfter) {
			throw new Error("开始时间不能为空");
		}
		if (!data.dutyDateBefore) {
			throw new Error("结束时间不能为空");
		}

		const url = `${this.config.baseUrl}/evo-apigw/evo-attendance/1.1.0/attendance/result/page`;
		const res = await this.curl<dahua.icc.IResponse<dahua.icc.attendance.IAttendanceResultResponse>>(url, {
			method: "post",
			headers: {
				Authorization: `bearer ${this.accessToken}`,
			},
			contentType: "json",
			dataType: "json",
			data,
		});
		this.handleResponseException(res);
		return res.data.data;
	}
}
